package com.tiantian.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tiantian.bean.PageBean;
import com.tiantian.dao.BackstageRoleRepository;
import com.tiantian.datajpa.service.impl.GenericServiceImpl;
import com.tiantian.domain.BackstageRole;
import com.tiantian.ret.Result;
import com.tiantian.service.BackstageRoleService;
import com.tiantian.util.CommonConstant.QueryType;
import com.tiantian.util.ConditionResult;
import com.tiantian.util.RoleUtils;

@Service
public class BackstageRoleServiceImpl extends GenericServiceImpl<BackstageRole, String> implements BackstageRoleService {
	private BackstageRoleRepository repository;

	@Autowired
	public void setRepository(BackstageRoleRepository repository) {
		super.setGenericDAO(repository);
		this.repository = repository;
	}

	/**
	 * 获取后台管理角色
	 * 
	 * @param params
	 * @param page
	 * @return
	 */
	public List<BackstageRole> list(Map<String, String> params, PageBean pageBean) {
		ConditionResult conditionResult = getQueryCondition(params, QueryType.LIST);
		List<BackstageRole> ret = repository.queryListBySql(conditionResult.getSql(), pageBean.getStartIndex(), pageBean.getPageSize(),
				conditionResult.getParams(), BackstageRole.class);
		conditionResult = getQueryCondition(params, QueryType.COUNT);
		pageBean.setTotal(repository.queryCountBySql(conditionResult.getSql(), conditionResult.getParams()));
		return ret;
	}

	private ConditionResult getQueryCondition(Map<String, String> searchMap, QueryType type) {
		StringBuilder sql = new StringBuilder();
		Map<String, Object> params = new HashMap<String, Object>();

		switch (type) {
		case LIST:
			sql.append(" select t.* ");

			break;
		case COUNT:
			sql.append(" select count(1)");
			break;
		default:
			break;
		}
		sql.append(" from BackstageRole t where 1=1 ");

		if (searchMap != null) {

		}
		if (type == QueryType.LIST) {
			sql.append(" order by t.createtime desc");
		}
		return new ConditionResult(sql.toString(), params);

	}

	/**
	 * 保存角色
	 * 
	 * @param name
	 * @param parameterMap
	 * @return
	 */
	public Result<Object> saveRole(String name, Map<String, Object> parameterMap) {
		BackstageRole role = new BackstageRole();
		role.setName(name);
		role.setAuthoritys(RoleUtils.formatAuthoritys(parameterMap, null));
		role.setCreatetime(System.currentTimeMillis());
		repository.save(role);
		return new Result<>(true, "保存角色成功");
	}

	/**
	 * 更新角色
	 * 
	 * @param id
	 * @param parameterMap
	 * @return
	 */
	public Result<Object> updateRole(String id, Map<String, Object> parameterMap) {
		BackstageRole role = repository.findOne(id);
		role.setAuthoritys(RoleUtils.formatAuthoritys(parameterMap, role.getId()));
		repository.save(role);
		return new Result<>(true, "更新角色成功");
	}

	@Override
	public List<BackstageRole> findByUserId(String id) {
		String sql = "select r.* from backstagerole r left join backstageuserrole ur on ur.roleId=r.id  where ur.userId=:userId";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userId", id);
		return repository.queryListBySql(sql, params, BackstageRole.class);
	}
}
